package com.qf;

import com.qf.dao.StudentDao;
import com.qf.pojo.Student;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;


public class test {
    SqlSession sqlSession;
    @Before
    public void before() throws IOException {
        InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
        sqlSession = factory.openSession();
    }

    @After
    public void after(){
        sqlSession.close();
    }
    @Test
    public void testQueryAll(){
        StudentDao studentDao = sqlSession.getMapper(StudentDao.class);
        List<Student> list = studentDao.queryAll();
        for (Student student : list) {
            System.out.println(student);
        }
    }

    @Test
    public void testQueryById(){
        StudentDao studentDao = sqlSession.getMapper(StudentDao.class);
        Student student = studentDao.queryById("10004");
        System.out.println(student);
    }

    @Test
    public void testQueryLimit(){
        StudentDao mapper = sqlSession.getMapper(StudentDao.class);
        List<Student> list = mapper.queryLimit(3, 5);
        for (Student student : list) {
            System.out.println(student);
        }
    }

    @Test
    public void testQueryCount(){
        StudentDao mapper = sqlSession.getMapper(StudentDao.class);
        long l = mapper.queryCount();
        System.out.println(l);
    }

    @Test
    public void testInsert(){
        StudentDao mapper = sqlSession.getMapper(StudentDao.class);
        Student student = new Student(0,"10008","傻兔叽","男",20);
        mapper.insert(student);
        sqlSession.commit();
        System.out.println(student.getSid());
    }

    @Test
    public void testUpdate(){
        StudentDao mapper = sqlSession.getMapper(StudentDao.class);
        Student student = new Student(0,"10008","蠢兔叽","女",10);
        mapper.update(student);
        sqlSession.commit();
    }

    @Test
    public void testDelete(){
        StudentDao mapper = sqlSession.getMapper(StudentDao.class);
        mapper.delete("10008");
        sqlSession.commit();
    }
}
